<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Media;

use App\Controller\Admin\AdminController;
use App\Core\Uploader;
use App\Helper\Utils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\Utils\Filesystem\Filesystem;

class UploadController extends AdminController
{
    /**
     * @Inject
     * @var Uploader
     */
    private $uploader;

    /**
     * @Inject
     * @var FilesystemFactory
     */
    private $filesystem;
    /**
     * @return \Psr\Http\Message\ResponseInterface
     */

    public function image()
    {
        $return = $this->uploader->uploadBySlice($this->request);
        if($return['success']){
            return $this->response->success($return);
        }
        return $this->response->error(10000);
    }

    public function fileTypes(){
        return $this->response->success(config('common.upload_file_type'));
    }

    public function file(){
        $qiniu = $this->filesystem->get('qiniu');
        $file = $this->request->file('file');
        $upload_dir = config('common.upload_dir_name').'/'.date('Ym').'/'.date('d').'/';
        $extension = strtolower($file->getExtension()) ?: 'png';
        $filename = Utils::createFileName().'.'.$extension;
        $result=$qiniu->put($upload_dir.$filename,file_get_contents($file->getRealPath()));
        return $this->response->success([
            'status' => 'done',
            'path' => '/'.$upload_dir.$filename,
            'url' => env('QINBIU_DOMAIN').'/'.$upload_dir.$filename
        ]);

    }
}
